/*------------------------------------------------------------------------------*\
**   Copyright 2008-2009 Jonhnny Weslley                                        **
**                                                                              **
**   Licensed under the Apache License, Version 2.0 (the "License");            **
**   you may not use this file except in compliance with the License.           **
**   You may obtain a copy of the License at                                    **
**                                                                              **
**       http://www.apache.org/licenses/LICENSE-2.0                             **
**                                                                              **
**   Unless required by applicable law or agreed to in writing, software        **
**   distributed under the License is distributed on an "AS IS" BASIS,          **
**   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   **
**   See the License for the specific language governing permissions and        **
**   limitations under the License.                                             **
\*------------------------------------------------------------------------------*/

package org.scalashell.terminal

object ANSI {

  val isSupported = {
      val UNDEFINED = "undefined"
      var enabled = jline.Terminal.getTerminal.isANSISupported
      val force = System.getProperty("org.scalashell.terminal.ANSI", UNDEFINED)
      if (force ne UNDEFINED) {
        enabled = force.toBoolean
      }
      enabled
  }

  def buffer = if (isSupported) new ANSIBuffer else new Buffer 


  object Code {

    // Text attributes
    val OFF = 0
    val BOLD = 1
    val UNDERSCORE = 4
    val BLINK = 5
    val REVERSE = 7
    val CONCEALED = 8
     
    // Foreground colors
    val FG_BLACK = 30
    val FG_RED = 31
    val FG_GREEN = 32
    val FG_YELLOW = 33
    val FG_BLUE = 34
    val FG_MAGENTA = 35
    val FG_CYAN = 36
    val FG_WHITE = 37

    // Background colors
    val BG_BLACK = 40
    val BG_RED = 41
    val BG_GREEN = 42
    val BG_YELLOW = 43
    val BG_BLUE = 44
    val BG_MAGENTA = 45
    val BG_CYAN = 46
    val BG_WHITE = 47

    // Colors
    val BLACK = FG_BLACK
    val RED = FG_RED
    val GREEN = FG_GREEN
    val YELLOW = FG_YELLOW
    val BLUE = FG_BLUE
    val MAGENTA = FG_MAGENTA
    val CYAN = FG_CYAN
    val WHITE = FG_WHITE


    import scala.collection.mutable.Map

    private val code2name: Map[Int, String] = Map()
    private val name2code: Map[String, Int] = Map()

    val methods = Code.getClass.getMethods
    for (method <- methods if (method.getName()(0)).isUpperCase) {
      val name = method.getName
      val code = method.invoke(this, null).asInstanceOf[Int]
      code2name(code) = name
      name2code(name) = code
    }

    implicit def apply(code: Int) = {
      if (!code2name.contains(code)) error("Invalid ANSI code: " + code) 
      code2name(code)
    }

    implicit def apply(name: String) = {
      val upperName = name.toUpperCase
      if (!name2code.contains(upperName)) error("Invalid ANSI code name: " + name)
      name2code(upperName)
    }

  }

}
